﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Notepad_X.Components
{
    /// <summary>
    /// Custom Tab Control
    /// </summary>
    public class TabControlX : System.Windows.Forms.TabControl
    {

        public TabControlX()
		{
            AllowDrop = true;
		}

		protected override void OnDragOver(System.Windows.Forms.DragEventArgs e)
		{
			base.OnDragOver(e);
			Point pt = new Point(e.X, e.Y);
			//We need client coordinates.
			pt = PointToClient(pt);

			//Get the tab we are hovering over.
            Notepad_X.Components.TabPageX hover_tab = GetTabPageByTab(pt);

			//Make sure we are on a tab.
			if(hover_tab != null)
			{
				//Make sure there is a TabPage being dragged.
                if (e.Data.GetDataPresent(typeof(Notepad_X.Components.TabPageX)))
				{
					e.Effect = DragDropEffects.Move;
                    Notepad_X.Components.TabPageX drag_tab = (Notepad_X.Components.TabPageX)e.Data.GetData(typeof(Notepad_X.Components.TabPageX));

					int item_drag_index = FindIndex(drag_tab);
					int drop_location_index = FindIndex(hover_tab);

					//Don't do anything if we are hovering over ourself.
					if(item_drag_index != drop_location_index)
					{
						ArrayList pages = new ArrayList();
						
						//Put all tab pages into an array.
						for(int i = 0; i < TabPages.Count; i++)
						{
							//Except the one we are dragging.
							if(i != item_drag_index)
								pages.Add(TabPages[i]);
						}

						//Now put the one we are dragging it at the proper location.
						pages.Insert(drop_location_index, drag_tab);

						//Make them all go away for a nanosec.
						TabPages.Clear();

						//Add them all back in.
                        TabPages.AddRange((Notepad_X.Components.TabPageX[])pages.ToArray(typeof(Notepad_X.Components.TabPageX)));
						
						//Make sure the drag tab is selected.
						SelectedTab = drag_tab;
					}
                }else if(e.Data.GetDataPresent(typeof(String))){
                    e.Effect = DragDropEffects.Copy;
                    SelectedTab = hover_tab;
				}

            } else if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
                e.Effect = DragDropEffects.All;
            } else {
                e.Effect = DragDropEffects.None;
            }
		}

        protected override void OnMouseDown(MouseEventArgs e) {
            base.OnMouseDown(e);

            if (e.Button == MouseButtons.Left && e.Clicks == 1) {
                Point pt = new Point(e.X, e.Y);
                Notepad_X.Components.TabPageX tp = GetTabPageByTab(pt);

                if (SelectedTab != tp && tp != null) {
                    base.SelectedIndex = base.TabPages.IndexOf(tp);
                } else {
                    if (tp != null) {
                        DoDragDrop(tp, DragDropEffects.Move);
                    }
                }
            }


        }
		/// <summary>
		/// Finds the TabPage whose tab is contains the given point.
		/// </summary>
		/// <param name="pt">The point (given in client coordinates) to look for a TabPage.</param>
		/// <returns>The TabPage whose tab is at the given point (null if there isn't one).</returns>
        public Notepad_X.Components.TabPageX GetTabPageByTab(Point pt)
		{
			for(int i = 0; i < TabPages.Count; i++)
			{
				if(GetTabRect(i).Contains(pt))
				{
                    if (TabPages[i].GetType() != typeof(Notepad_X.Components.TabPageX)) return null;
                    return (Notepad_X.Components.TabPageX) TabPages[i];
				}
			}
			return null;
		}
        public Notepad_X.Components.TabPageX GetTabPageByID(int id)
        {
            for (int i = 0; i < TabPages.Count; i++)
            {
                if (((Notepad_X.Components.TabPageX)TabPages[i]).id == id)
                {
                    return (Notepad_X.Components.TabPageX)TabPages[i];
                }
            }
            return null;
        }

		/// <summary>
		/// Loops over all the TabPages to find the index of the given TabPage.
		/// </summary>
		/// <param name="page">The TabPage we want the index for.</param>
		/// <returns>The index of the given TabPage(-1 if it isn't found.)</returns>
        private int FindIndex(Notepad_X.Components.TabPageX page)
		{
			for(int i = 0; i < TabPages.Count; i++)
			{
				if(TabPages[i] == page) return i;
			}
			return -1;
		}

        //protected override void OnDrawItem(DrawItemEventArgs e) {
        //    base.OnDrawItem(e);

        //    e.DrawBackground();


        //    e.DrawFocusRectangle();
        //    e.DrawBackground();


        //    e.Graphics.DrawString(this.TabPages[e.Index].Text, new Font("Arial", 8), new SolidBrush(Color.Black), e.Bounds.X + 24, (e.Bounds.Height / 2) - 8);

        //    if (!String.IsNullOrEmpty(this.TabPages[e.Index].ImageKey)) {
        //        Image img = this.ImageList.Images[this.TabPages[e.Index].ImageKey];

        //        RectangleF imgRec = new RectangleF(e.Bounds.X, e.Bounds.Y, img.Width, img.Height);
        //        e.Graphics.DrawImage(img, imgRec);
        //    }
        //}
        
	}
}

